<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use Intervention\Image\ImageManagerStatic as Image;
use File;
use App\Models\Admin\Product;
use Storage;
use App\Models\Admin\Photo;
use App\Models\Admin\Procate;
class ProductsController extends Controller
{
    //商品列表页面展示
    public function index(){
        $data = Product::get();
        //$photo =Photo::get();
 
        //SELECT wt_product.* ,wt_photo.photo as parent_name FROM wt_product   LEFT JOIN wt_photo on wt_product.product_no=wt_photo.product_no;
    
 		return view('admin.product.index',compact('data'));

    }

    public function onephoto(Request $request){


        $photo = Photo::where('product_no',$request->input('product_no'))->limit('1')->get()->toArray();
       //var_dump($photo);die();
        return $photo[0]['photo'];


    }
    //添加商品页面
    public function add(Request $request){

        if ($request->isMethod('post')) {
           
            //var_dump($request->input('avatar'));die();
           
            $result = Product::insert([

                'name'  => $request->input('name'),
                'product_no' => $request->input('product_no'),
        
                'category_id'    => $request->input('category_id'),
                'sort'=> $request->input('sort'),
                'spec'     => $request->input('spec'),
                'place'    => $request->input('place'),
                'unit'     => $request->input('unit'),
                'price'    => $request->input('price'),  
                'keywords' => $request->input('keywords'), 
                'description' => $request->input('description'),                 
                'thumb' =>  $request->input('avatar')[0],
                'contents' =>$request->input('contents'),
              

            ]);
            if (!empty($request->input('avatar'))) {
                
                foreach ($request->input('avatar') as $key => $value) {
                
                    Photo::insert([
                    'product_no' => $request->input('product_no'),
                    'photo' => $value,

                ]);
              }
            }

            return $result ? '1' : '0';
        }else{

            $result = Procate::get();
            return view('admin.product.add',compact('result'));

        }
 	

    }


    //多图商品图册上传
    public function upload(Request $request)
    {
      

     $path = 'storage/product/';
 
     if ($request->hasFile('file') && $request->file('file')->isValid()) {
     	
 		$photo = $request->file('file');
        //var_dump($photo);
        $file_name = uniqid().'.'.$photo->getClientOriginalExtension();
        $file_path =public_path($path);
        $thumbnail_file_path = $file_path .$file_name;
        $image = Image::make($photo)->save($thumbnail_file_path);
        //echo '上传成功';resize(700, 640, function ($constraint) {$constraint->aspectRatio();})->
  
        $result = [ 
            'errCode'  => '0',
            'errMsg'   => '',
            'succMsg'  => '文件上传成功！',
            'path'     => '/'.$path.$file_name,
            
         
        ];
 	
          
        }
      
        else {
            $result = [
                'errCode'  => '0000001',
                'errMsg'   => $request ->file('file')->getErrorMessage(),

            ];
           
        }
        return response()-> json($result);
    }





    //添加图册图片
    public function addpic(Request $request)
    {
     
     $path = 'storage/product/';
 
     if ($request->hasFile('file') && $request->file('file')->isValid()) {
        
       $photo = $request->file('file');
        //var_dump($photo);
        $file_name = uniqid().'.'.$photo->getClientOriginalExtension();
        $file_path =public_path($path);
        $thumbnail_file_path = $file_path .$file_name;
        $image = Image::make($photo)->save($thumbnail_file_path);
        //echo '上传成功';resize(700, 640, function ($constraint) {$constraint->aspectRatio();})->
  
        $result = [ 
            'errCode'  => '0',
            'errMsg'   => '',
            'succMsg'  => '文件上传成功！',
            'path'     => '/'.$path.$file_name,
            
         
        ];
    
          
        }
      
        else {
            $result = [
                'errCode'  => '0000001',
                'errMsg'   => $request ->file('file')->getErrorMessage(),

            ];
           
        }
        return response()-> json($result);
    }
    //上传商品缩略图
    public function thumbadd(Request $request){

        if ($request->hasFile('file') && $request->file('file')->isValid()) {
        
        $photo = $request->file('file');
        $file_name = uniqid().'.'.$photo->getClientOriginalExtension();
        $file_path =public_path('storage/thumbs/');
        $thumbnail_file_path = $file_path .$file_name;
        $image = Image::make($photo)->resize(350, 220, function ($constraint) {$constraint->aspectRatio();})->save($thumbnail_file_path);
        //echo '上传成功';
  
        $result = [ 
            'errCode'  => '0',
            'errMsg'   => '',
            'succMsg'  => '文件上传成功！',
            'path'     => '/storage/thumbs/'.$file_name,
         
        ];
            
        }else{

              $result = [
                'errCode'  => '0000001',
                'errMsg'   => $request ->file('file')->getErrorMessage(),

            ];
         
            
        }
         return response()-> json($result);

      
     }
    // 上传商品缩略图界面 
    public function thumb(Request $request){

        if ($request->isMethod('post')) {
            
            $result = Product::where('id',$request->input('id'))->update([

            'thumb'  => $request->input('thumb'),
         

         ]);
        return $result ? '1' : '0';
        }
        else {
            
            return view('admin.product.thumbadd');

        }
    }
    //商品编辑页面
    public function edit(Request $request){

        if ($request->isMethod('post')) {
            $result = Product::where('id',$request->input('id'))->update([

                'name'  => $request->input('name'),
                'product_no' => $request->input('product_no'),
        
                'category_id'    => $request->input('category_id'),
                'sort'=> $request->input('sort'),
                'spec'     => $request->input('spec'),
                'place'    => $request->input('place'),
                'unit'     => $request->input('unit'),
                'price'    => $request->input('price'),  
                'keywords' => $request->input('keywords'), 
                'description' => $request->input('description'),                 
                
                'contents' =>$request->input('contents'), 

            ]);
            if (!empty($request->input('photo'))) {
             
                foreach ($request->input('photo') as $key => $value) {
                
                    Photo::insert([
                    'product_no' => $request->input('product_no'),
                    'photo' => $value,
                    ]);
                }
            }

            return $result ? '1' : '0';
        }
        else{

            //  SELECT wt_product.* ,wt_procate.name as parent_name FROM wt_product   LEFT JOIN wt_procate on wt_product.category_id=wt_procate.id;
            $data = DB::table('wt_product')->where('wt_product.id',$request->input('id'))->select('wt_product.*','wt_procate.name as parent_name')->leftJoin('wt_procate', 'wt_product.category_id' ,'=','wt_procate.id')->get();
            //var_dump($data);die();

         
            $pic = Photo::where('product_no',$request->input('product_no'))->get();
            $result = Procate::get();
            //$data = Product::where('id',$request->input('id'))->get();
            return view('admin.product.edit',compact('data','pic','result'));
        }
    }
    //删除图册图片
    public function deletepic(Request $request){
        
   
        $result = Photo::where('id',$request->only('id'))->delete();
        return $result ? '1':'0';
    }
    //删除商品
    public function delproducts(Request $request){
        $result = Product::where('id',$request->only('id'))->delete();
        return $result ? '1':'0';

    }
    //展示商品分类
    public function procategory(){


        $result = Procate::get();
        return view('admin.procate.index',compact('result'));
    }
    //添加商品分类
    public function procateadd(Request $request){
        if ($request->isMethod('post')) {
            
             $result = Procate::insert([

            'name'  => $request->input('name'),
            'sort' => $request->input('sort'),      
            
            'content' =>$request->input('content'),
           ]);

        return $result ? '1':'0';
        
        }else{

            return view('admin.procate.add');
        }

    }
   
}
